package com.yndsoft.jc.supplier.main;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.SudokuAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.AccountModel;
import com.yndsoft.jc.supplier.model.HeadModel;
import com.yndsoft.jc.supplier.model.SudokuModel;
import com.yndsoft.jc.supplier.model.SupplierInfoModel;
import com.yndsoft.jc.supplier.model.SupplierModel;
import com.yndsoft.jc.supplier.net.ApiService;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.GlideCircleTransform;
import com.yndsoft.jc.supplier.widget.recyclerview.DividerGridItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class SupplierActivity extends SupplierBaseActivity {

    private ImageView orgHead;
    private TextView orgName;
    private TextView orgLive;
    private TextView orgCount;
    private TextView orgIncome;
    private TextView orgBalance;
    private LinearLayout orgNumber;
    private RecyclerView orgSudoku;
    private String tag;

    @Override
    public int getContentResId() {
        return R.layout.activity_org;
    }

    public static void start(Context context, String id, int tid, String sid, String name) {
        Intent intent = new Intent(context, SupplierActivity.class);
        intent.putExtra(Constants.SUPPLIER_ID, id);
        intent.putExtra(Constants.SUPPLIER_TID, tid);
        intent.putExtra(Constants.SUPPLIER_SID, sid);
        intent.putExtra(Constants.SUPPLIER_NAME, name);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        orgHead = findViewById(R.id.org_head);
        orgName = findViewById(R.id.org_name);
        orgLive = findViewById(R.id.org_live);
        orgCount = findViewById(R.id.org_count);
        orgIncome = findViewById(R.id.org_income);
        orgBalance = findViewById(R.id.org_balance);
        orgNumber = findViewById(R.id.org_number);
        orgSudoku = findViewById(R.id.org_sudoku);
    }

    @Override
    public void initData() {

//        mHttpUtil.supplierInfo(new ProgressSubscriber(mContext) {
//            @Override
//            public void next(ReturnMsg returnMsg) {
//                String info = returnMsg.getInfo();
//                if (info != null) {
//                    SupplierInfoModel model = new Gson().fromJson(info, SupplierInfoModel.class);
//                    if (model != null) {
//                        Glide.with(mContext)
//                                .load(ApiService.BASE_URL + model.getImg())
//                                .transform(new GlideCircleTransform(mContext))
//                                .placeholder(R.mipmap.product_img)
//                                .into(orgHead);
//                    }
//                }
//            }
//        }, "supplier", mSession.getSid());

        String supplierId = getIntent().getStringExtra(Constants.SUPPLIER_ID);
        int supplierTid = getIntent().getIntExtra(Constants.SUPPLIER_TID, 0);
        String supplierSid = getIntent().getStringExtra(Constants.SUPPLIER_SID);
        String supplierName = getIntent().getStringExtra(Constants.SUPPLIER_NAME);

        mCache.put(Constants.SUPPLIER_ID, supplierId);
        mCache.put(Constants.SUPPLIER_SID, supplierSid);
        mCache.put(Constants.SUPPLIER_TID, supplierTid);

        orgName.setText(supplierName != null ? supplierName : "");

        List<SudokuModel> sudokuList = new ArrayList<>();
        sudokuList.add(new SudokuModel("订单列表", R.mipmap.order_organization));
        switch (supplierTid) {
            case 1: // 管理员
                tag = Constants.SUPPLIER;
                orgNumber.setVisibility(View.VISIBLE);
                loadData(supplierSid);
                sudokuList.add(new SudokuModel("评价列表", R.mipmap.assessment_organization));
                sudokuList.add(new SudokuModel("完善项目", R.mipmap.items_organization));
                sudokuList.add(new SudokuModel("假期列表", R.mipmap.holiday_organization));
                sudokuList.add(new SudokuModel("机构资料", R.mipmap.information_organization));
                sudokuList.add(new SudokuModel("修改密码", R.mipmap.changepassword_organization));
                sudokuList.add(new SudokuModel("联系人", R.mipmap.contacts_organization));
                sudokuList.add(new SudokuModel("录入员", R.mipmap.keyboarder_organization));
                sudokuList.add(new SudokuModel("审核员", R.mipmap.assessor_organization));
                Glide.with(mContext).load(R.mipmap.product_img).transform(new GlideCircleTransform(mContext)).into(orgHead);
                break;
            case 2: // 录入员
                tag = Constants.ENTRY;
                orgNumber.setVisibility(View.GONE);
//                sudokuList.add(new SudokuModel("个人资料", R.mipmap.information_organization));
                Glide.with(mContext).load(R.mipmap.entry_img).transform(new GlideCircleTransform(mContext)).into(orgHead);
                break;
            case 3: // 审核员
                tag = Constants.SIGN;
                orgNumber.setVisibility(View.GONE);
//                sudokuList.add(new SudokuModel("个人资料", R.mipmap.information_organization));
                Glide.with(mContext).load(R.mipmap.sign_img).transform(new GlideCircleTransform(mContext)).into(orgHead);
                break;
        }
        sudokuList.add(new SudokuModel("设置", R.mipmap.set_organization));
        orgSudoku.setLayoutManager(new GridLayoutManager(mContext, 3));
        orgSudoku.setAdapter(new SudokuAdapter(mContext, sudokuList, tag));
        orgSudoku.addItemDecoration(new DividerGridItemDecoration(mContext));
    }

    private void loadData(String supplierSid) {
        mHttpUtil.supplierInfo(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        break;
                    case 1:
                        String msg = returnMsg.getInfo();
                        if (msg != null) {
                            SupplierModel model = new Gson().fromJson(msg, SupplierModel.class);
                            orgLive.setText(model.getLivenum() != null ? model.getLivenum() : "");
                            orgCount.setText(model.getAllnum() != null ? model.getAllnum() : "");
                            orgIncome.setText(model.getAccount() != null ? model.getAccount() : "");
                            orgBalance.setText(model.getAllaccount() != null ? model.getAllaccount() : "");
                        }
                        break;
                }
            }
        }, "supplier", supplierSid);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void onEvent(HeadModel model) {
        if (model != null && 1 == model.getCode()) {
            Glide.with(mContext)
                    .load(ApiService.BASE_URL + model.getHead())
                    .transform(new GlideCircleTransform(mContext))
                    .placeholder(R.mipmap.product_img)
                    .into(orgHead);
            orgName.setText(model.getName() != null ? model.getName() : "");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        onEvent(new HeadModel());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}